<template>
  <div class="cartDetails">
    <!-- 商品的列表 -->
    <div class="content">
      <van-checkbox-group v-model="result" @change="groupChange">
        <div v-for="(i, index) in $store.state.cartList">
          <FoodAdd :item="i" :showCheckbox="true" :onChange="onChange" />
        </div>
      </van-checkbox-group>
    </div>
    <!-- 结算 -->
    <van-submit-bar
      :price="allPrice * 100"
      button-text="结算"
      @submit="onSubmit"
      class="submit-all"
      button-color="#ffc400"
      v-if="isDelete"
    >
      <van-checkbox v-model="checked" checked-color="#ffc400" @click="choseAll">
        全选
      </van-checkbox>
    </van-submit-bar>

    <!-- 删除 -->
    <div class="buy" v-else>
      <div class="left">
        <van-checkbox
          v-model="checked"
          checked-color="#ffc400"
          @click="choseAll"
        >
          全选
        </van-checkbox>
      </div>
      <div class="delete" @click="deleteClick">删除</div>
    </div>
  </div>
</template>

<script>
import FoodAdd from "../../../components/FoodAdd.vue";
import {Toast} from "vant";
export default {
  components: { FoodAdd },
  props: ["changeShow"],
  data(){
    return{
       result: [], 
       checked: true,
       isDelete: true,
    }
  },
  mounted(){
    this.init();

    // 监听编辑的点击
    this.$bus.$on("edit", () => {
      this.isDelete = !this.isDelete;
    });

  },
  computed:{
    // 总价
    allPrice(){
      let countList = this.updata(2);
      let sum = 0;
      countList.forEach((item) => {
        sum += item.num * item.price;
      });
      return sum;      
    },
  },
  methods: {
    // 商品默认选中的初始化
    init(){
      this.result = this.$store.state.cartList.map((item) => item.id);
    },

    // 商品的个数同步
    onChange(value, detail){
      this.$store.state.cartList.map((item) => {
        if (item.id === detail.name) {
          item.num = value;
        }
      });
    },

    // 全选或者取消全选
    choseAll(){
      if (this.result.length !== this.$store.state.cartList.length) {
        this.init();
      } else {
        this.result = [];
      }
    },

    // 更新数据
    updata(type){
      return this.$store.state.cartList.filter((item) => {
        return type === 2
          ? this.result.includes(item.id)
          : !this.result.includes(item.id);
      });
    },

    // 结算按钮
    onSubmit(){
      if (this.result.length !== 0) {
        this.$store.commit("PAY", this.updata(2));
        this.$router.push({
          path: "/createorder",
          query: {
            list: this.result,
          },
        });
      } else {
        Toast.fail("请选择要结算的商品");
      }          
    },

    // 每个复选框的点击事件触发
    groupChange(result){
      if (result.length === this.$store.state.cartList.length) {
        this.checked = true;
      } else {
        this.checked = false;
      }
      this.result = result;
    },

    // 删除按钮
    deleteClick(){
      if (this.result.length) {
        // 更新删除后的购物车的数据
        this.$store.commit("DELETE", this.updata(1));

        // 删除后的选中
        this.result = [];

        // 购物车无数据时展示兜底
        if (this.$store.state.cartList.length === 0) {
          this.$store.commit("EDIT", "delete");
          //props
          this.changeShow();
        }
      } else {
        Toast.fail("请选择要删除的商品");
      }
    },
  },
};
</script>

<style lang='less' scoped>
.cartDetails {
  font-size: 14px;
  flex: 1;
  position: relative;
  overflow-y: auto;
  padding: 20px 20px 55px;
  .submit-all {
    position: fixed;
    bottom: 48px;
  }

  .buy {
    position: fixed;
    bottom: 48px;
    right: 0;
    display: flex;
    justify-content: space-between;
    width: 100%;
    background-color: #fff;
    border-radius: 10px;
    height: 50px;
    align-items: center;
    padding: 0 16px;
    box-sizing: border-box;
    .left {
      display: flex;
      align-items: center;
    }
    .delete {
      color: #fff;
      background-color: #ffc400;
      border-radius: 20px;
      font-size: 14px;
      font-weight: 600;
      width: 110px;
      height: 40px;
      text-align: center;
      line-height: 40px;
    }
  }

  .content {
    padding: 10px;
    background-color: #fff;
    border-radius: 10px;
  }
}
</style>